﻿using AutoMapper;
using MediatR;
using MyMES.Domain.yjb;
using MyMES.Domain.zmc;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Productionwork;
using MyMES.Read.API.Dtos;
using MySqlX.XDevAPI.Common;

namespace MyMES.Read.API.Application.CommendHandlers.Productionwork
{
    public class KanbanCommendHandlers : IRequestHandler<KanbanCommends, APIResult<List<KanbanDtos>>>
    {

        private readonly IBaseRepository<Process> processRepository;
        //Requisition
        private readonly IBaseRepository<WorkOrder>  workorderRepository;
        private readonly IBaseRepository<Requisition>  reqRepository;
      
        private readonly IBaseRepository<Arrivalnotice>  arrivalnoticRepository;
        private readonly IBaseRepository<ProcessInfo> processinfosRepository;
        private readonly IMapper mapper;

        public KanbanCommendHandlers(IBaseRepository<Process> processRepository, IBaseRepository<WorkOrder> workorderRepository, IBaseRepository<Requisition> reqRepository, IBaseRepository<Arrivalnotice> arrivalnoticRepository, IBaseRepository<ProcessInfo> processinfosRepository, IMapper mapper)
        {
            this.processRepository = processRepository;
            this.workorderRepository = workorderRepository;
            this.reqRepository = reqRepository;
            this.arrivalnoticRepository = arrivalnoticRepository;
            this.processinfosRepository = processinfosRepository;
            this.mapper = mapper;
        }



        /// <summary>
        /// 工单执行进度看板
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<KanbanDtos>>> Handle(KanbanCommends request, CancellationToken cancellationToken)
        {
            //
            APIResult<List<KanbanDtos>> result = new APIResult<List<KanbanDtos>>();

            result.Code = APIEnum.查询成功;
            result.Msg = "查询成功";
            var sql = "SELECT a.*,b.WorkOrderCoding,b.State,c.RequisitionNum,c.RequisitionName,d.ProcessName,d.ProcessCode,e.QualifiedNum FROM process a LEFT JOIN workorder b on a.WorkOrderId=b.WorkOrderId  LEFT JOIN requisition c on a.Requisitionid=c.Requisitionid   LEFT JOIN processinfo d on a.ProcessInfoId=d.ProcessInfoId  LEFT JOIN   arrivalnotice  e on a.NoticeId=e.NoticeId   where IsDlete=false ORDER BY a.ProcessID DESC;";

            var process = await processRepository.SelectAsync(sql);
            var arrivas = await arrivalnoticRepository.SelectAsync(sql);
            var workorders = await workorderRepository.SelectAsync(sql);
            var processinfo = await processinfosRepository.SelectAsync(sql);
            var reqs = await reqRepository.SelectAsync(sql);

            var proces = mapper.Map<List<KanbanDtos>>(process);
            var rarriva = mapper.Map<List<KanbanDtos>>(arrivas);
            var workorderss = mapper.Map<List<KanbanDtos>>(workorders);
            var processinfos = mapper.Map<List<KanbanDtos>>(processinfo);
            var reqss = mapper.Map<List<KanbanDtos>>(reqs);


            //循环赋值
            foreach (var item in proces)
            {

                item.WorkOrderCoding = workorderss.FirstOrDefault(x => x.WorkOrderId == item.WorkOrderId)?.WorkOrderCoding;
                item.State = workorderss.FirstOrDefault(x => x.WorkOrderId == item.WorkOrderId)?.State;

                item.RequisitionNum = reqss.FirstOrDefault(x => x.Requisitionid == item.Requisitionid)?.RequisitionNum;
                item.RequisitionName = reqss.FirstOrDefault(x => x.Requisitionid == item.Requisitionid)?.RequisitionName;

                item.ProcessName = processinfos.FirstOrDefault(x => x.ProcessInfoId == item.ProcessInfoId).ProcessName;
                item.ProcessCode = processinfos.FirstOrDefault(x => x.ProcessInfoId == item.ProcessInfoId).ProcessCode;

                item.QualifiedNum = rarriva.FirstOrDefault(x => x.NoticeId == item.NoticeId).QualifiedNum;

            }

            result.data = proces;
            return   result;
        }
    }
}
